// Copyright (c) 2023 Presto Labs Pte. Ltd.
// Author: tianqiang

#pragma once

#include "coin2/exchange/base/order/order.h"
#include "coin2/exchange/bittrex/api_util/rate_tracker.h"
#include "coin2/exchange/bittrex/symbology/product.h"
#include "coin2/exchange/order/bittrex.pb.h"

namespace coin2::exchange::bittrex::api_order {

class BittrexNativePrivateClient : public coin2::exchange::base::order::NativePrivateClient {
 public:
  using RestParams = coin2::exchange::order::BittrexRestParams;
  using NativeClientConfig = coin2::exchange::order::OrderConnectionConfig;
  using ActionType = coin2::exchange::base::order::NativePrivateClient::ActionType;
  using BittrexRateTracker = coin2::exchange::bittrex::api_util::BittrexRateTracker;
  using BittrexProduct = coin2::exchange::bittrex::symbology::BittrexProduct;

 public:
  explicit BittrexNativePrivateClient(
      const MarketExchangeApi& mea,
      const NativeClientConfig& config);

  std::shared_ptr<AsioHttpContext> QueryAccountBalance();
  std::shared_ptr<AsioHttpContext> QueryOpenOrders();
  std::shared_ptr<AsioHttpContext> QueryHistoryOrders();
  std::shared_ptr<AsioHttpContext> QueryFills();
  std::shared_ptr<AsioHttpContext> GenListenKey(bool is_init);

  RestParams GetRestOrderParams(const OrderSpec& order_spec, int64_t proc_order_id);
  RestParams GetRestCancelParams(const IOrderContext& order_context);
  std::shared_ptr<AsioHttpContext> PlaceOrder(const RestParams& params);
  std::shared_ptr<AsioHttpContext> CancelOrder(const RestParams& params);
  std::shared_ptr<AsioHttpContext> PlaceOrderNorm(const OrderExecution&) override;
  std::shared_ptr<AsioHttpContext> CancelOrderNorm(const OrderExecution&) override;

  void Prepare();
  std::string DebugString() const;

 private:
  std::string GetUUIDByProcOrderId(int64_t proc_order_id);

  int64_t start_time_;
  std::string listen_key_;
  std::unordered_map<std::string, int32_t> api_block_info_;
  std::unordered_map<std::string, int32_t> api_req_info_;
  BittrexRateTracker* local_rate_tracker_;
};

}  // namespace coin2::exchange::bittrex::api_order
